// Copyright (C) 2009, Jackie Ng
// http://code.google.com/p/mgfdo, jumpinjackie@gmail.com
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
//
// See license.txt for more/additional licensing information

#include "MgConnection.h"
#include "MgDescribeSchemaCommand.h"
#include "MgGetClassNamesCommand.h"
#include "MgGetSpatialContextsCommand.h"
#include "MgSelectCommand.h"

MgConnection::MgConnection() : m_refCount(1) 
{ 
	mCaps = new MgCapabilities();
	mConnInfo = new MgConnectionInfo(this);
}

MgConnection::~MgConnection() 
{ 
	delete mCaps;
	delete mConnInfo;
}

FdoICommandCapabilities* MgConnection::GetCommandCapabilities()
{
	return FDO_SAFE_ADDREF(mCaps);
}

FdoIConnectionCapabilities* MgConnection::GetConnectionCapabilities()
{
	return FDO_SAFE_ADDREF(mCaps);
}

FdoIExpressionCapabilities* MgConnection::GetExpressionCapabilities()
{
	return FDO_SAFE_ADDREF(mCaps);
}

FdoIFilterCapabilities* MgConnection::GetFilterCapabilities()
{
	return FDO_SAFE_ADDREF(mCaps);
}

FdoIGeometryCapabilities* MgConnection::GetGeometryCapabilities()
{
	return FDO_SAFE_ADDREF(mCaps);
}

FdoIRasterCapabilities* MgConnection::GetRasterCapabilities()
{
	return FDO_SAFE_ADDREF(mCaps);
}

FdoISchemaCapabilities* MgConnection::GetSchemaCapabilities()
{
	return FDO_SAFE_ADDREF(mCaps);
}

FdoITopologyCapabilities* MgConnection::GetTopologyCapabilities()
{
	return FDO_SAFE_ADDREF(mCaps);
}

FdoITransaction* MgConnection::BeginTransaction()
{
	return NULL;
}

void MgConnection::Close()
{
	mState = FdoConnectionState_Closed;
}

FdoICommand* MgConnection::CreateCommand(FdoInt32 commandType)
{
	switch(commandType)
	{
		case FdoCommandType_DescribeSchema:
			return new MgDescribeSchema(this);
		case FdoCommandType_GetClassNames:
			return new MgGetClassNames(this);
		case FdoCommandType_GetSpatialContexts:
			return new MgGetSpatialContexts(this);
		case FdoCommandType_Select:
			return new MgSelect(this);
	//case FdoCommandType_SelectAggregates:
	}
	throw FdoException::Create(L"Command not supported");
}

FdoPhysicalSchemaMapping* MgConnection::CreateSchemaMapping()
{
	return NULL;
}

void MgConnection::Flush()
{

}

FdoIConnectionInfo* MgConnection::GetConnectionInfo()
{
	return FDO_SAFE_ADDREF(mConnInfo);
}

FdoConnectionState MgConnection::GetConnectionState()
{
	return mState;
}

FdoString* MgConnection::GetConnectionString()
{
	return L"";
}

FdoInt32 MgConnection::GetConnectionTimeout()
{
	return -1;
}

FdoConnectionState MgConnection::Open()
{
	mState = FdoConnectionState_Open;
	return mState;
}

void MgConnection::SetConfiguration(FdoIoStream* configStream)
{
}

void MgConnection::SetConnectionString(FdoString* value)
{
}

void MgConnection::SetConnectionTimeout(FdoInt32 value)
{
}

MgSpatialContextReader* MgConnection::GetSpatialContexts()
{
	return NULL;
}

FdoFeatureSchemaCollection* MgConnection::DescribeSchema(const FdoString* schemaName, const FdoStringCollection* classNames)
{
	FdoFeatureSchemaCollection* schemas = FdoFeatureSchemaCollection::Create(NULL);

	return FDO_SAFE_ADDREF(schemas);
}

FdoStringCollection* MgConnection::GetSchemaNames()
{
	FdoStringCollection* schemaNames = FdoStringCollection::Create();

	return FDO_SAFE_ADDREF(schemaNames);
}

FdoStringCollection* MgConnection::GetClassNames(const FdoString* schemaName)
{
	FdoStringCollection* classNames = FdoStringCollection::Create();

	return FDO_SAFE_ADDREF(classNames);
}